//
// Created by DELL on 2024/10/29.
//
#include <thread>
#include <iostream>
#include <condition_variable>
#include <mutex>
#include <atomic>

int main()
{
    std::mutex lock;
    std::condition_variable cv;
    bool isZero(true);

    std::thread thread0([&] {

        while (true)
        {
            {
                std::unique_lock<std::mutex> lk(lock);
                cv.wait(lk, [&] { return isZero; });
                std::this_thread::sleep_for(std::chrono::seconds(1));

                std::cout << "thread0: " << "0" << std::endl;
                isZero = false;
            }
            cv.notify_one();
        }
    });

    std::thread thread1([&] {
        while (true)
        {
            {
                std::unique_lock<std::mutex> lk(lock);
                cv.wait(lk, [&] { return !isZero; });
                std::this_thread::sleep_for(std::chrono::seconds(1));

                std::cout << "thread1: " << "1" << std::endl;
                isZero= true;
            }
            cv.notify_one();
        }
    });

    thread0.join();
    thread1.join();

    return 0;
}
